input=$1
if [ $# -eq 0 ]
then
  echo "No arguments supplied"
  echo "Please provide path of host port list"
  exit 1
fi
# telnet timeout in 5 seconds, kill
while read -r line || [ -n "$line" ];
do
  host=$(echo $line | cut -f1 -d " ")
  port=$(echo $line | cut -f2 -d " ")
  echo -e "\x1d close \x0d" | 
  timeout --signal=9 5 telnet $host $port 2>error.txt && 
  echo "${host}:${port} is open" | tee -a ./$(hostname)-connected.txt
  if [ ! $? -eq 0 ]
  then 
    STATUS=$(grep "refused" error.txt)
    if [ ${#STATUS} -eq 0 ]
    then
      echo "${host}:${port} is timeout" | tee -a ./$(hostname)-timeout.txt
    else
      echo "${host}:${port} is refused" | tee -a ./$(hostname)-refused.txt
    fi
  fi
done < "$input"